package com.tql.mall.common.exception;

import com.tql.mall.common.constant.ExceptionConstant;
import com.tql.mall.common.result.RC;
import io.swagger.annotations.ApiModel;
import io.swagger.annotations.ApiModelProperty;
import lombok.Data;

/**
 * @Author: tyk
 * @Date: 2023/5/20 20:33
 * @Description: 自定义全局异常类
 */
@Data
@ApiModel(value = "自定义全局异常类")
public class MallException extends RuntimeException {

    @ApiModelProperty(value = "异常状态码")
    private Integer code;

    /**
     * @param code 状态码
     * @param msg  错误信息
     */
    public MallException(Integer code, String msg) {
        super(msg);
        this.code = code;
    }

    /**
     * @param rc 返回结果状态信息
     */
    public MallException(RC rc) {
        super(rc.getMsg());
        this.code = rc.getCode();
    }

    @Override
    public String toString() {
        return String.format(ExceptionConstant.MSG_TEMPLATE, code, this.getMessage());
    }
}
